package com.cfxy.dao.daoImply;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.cfxy.dao.IBaseDao;
import com.cfxy.model.Message;
import com.cfxy.model.User;
import com.cfxy.util.JdbcUtil;


public class MessagerDaoImpl implements IBaseDao<Message,Integer> {
	Connection conn = new JdbcUtil().getConnection();
	String sql =null;
	
	@Override
	public boolean insert(Message gb) {
		int x = 0 ;
		try {
			Timestamp ts = new Timestamp(System.currentTimeMillis());
			String y=new SimpleDateFormat("yyyy/MM/dd hh:mm:ss").format(ts);
			System.out.println(y);
			sql = "insert into message(title,time,content) values(?,'"+y+"',?)";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1,gb.getTitle());
			ps.setString(2,gb.getContent());
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(x>0) return true;
		else return false;
	}

	@Override
	public boolean update(Message obj) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean delete(Integer id) {
		PreparedStatement ps = null;
		String sql = "delete from message where id = ?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1,id);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public Message get(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Message> getall() {
		List<Message> list = new ArrayList<Message>();
		PreparedStatement ps = null;
		String sql = "select * from message";
		try {
			ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				Message m = new Message();
				m.setId(rs.getInt("id"));
				m.setName(rs.getString("name"));
				m.setTime(rs.getString("time"));
				m.setTitle(rs.getString("title"));
				m.setContent(rs.getString("content"));
				list.add(m);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Message> findSql(String sql) {
		
		return null;
	}

}
